<template>
  <el-form
    ref="ruleFormRef"
    :model="tsForm"
    :rules="rules"
    class="dets-ruleForm"
    label-position="top"
    size="small"
  >
    <el-form-item
      :label="t('plc.acknowledgementRequired')"
      prop="acknowledgementRequired"
    >
      <el-switch v-model="tsForm.acknowledgementRequired" />
    </el-form-item>
    <el-form-item
      :label="t('plc.com')"
      prop="payload.com"
      placeholder="组件编号"
    >
      <el-input
        v-model="tsForm.payload.com"
        @keyup.enter="submitForm(ruleFormRef)"
      />
    </el-form-item>
    <el-form-item :label="t('plc.ti')" prop="payload.ti" placeholder="任务状态">
      <el-input
        v-model="tsForm.payload.ti"
        @keyup.enter="submitForm(ruleFormRef)"
      />
    </el-form-item>
    <el-form-item>
      <el-button
        type="primary"
        @click="submitForm(ruleFormRef)"
        :disabled="!plcServerStatus"
        :loading="loading"
      >
        {{ t('plc.send') }}
      </el-button>
      <el-button @click="resetForm(ruleFormRef)">
        {{ t('plc.reset') }}
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { nextTick, onMounted, reactive, ref } from 'vue'
import { ElInput, ElMessage, FormInstance, FormRules } from 'element-plus'
import { useI18n } from 'vue-i18n'
import { sendMessageByServer } from '@/api/plc'
import { plcStore } from '@/store/plcStore'
import { storeToRefs } from 'pinia'

const store = plcStore()
const loading = ref(false)
const { plcServerStatus, plcClientIp, plcClientPort } = storeToRefs(store) // 连接状态
const { t } = useI18n()
const ruleFormRef = ref<FormInstance>()
const inputValue = ref()
const inputVisible = ref(false)
const InputRef = ref<InstanceType<typeof ElInput>>()

const props = defineProps({
  ip: {
    type: String,
    required: false,
    default: '127.0.0.1',
  },
  port: {
    type: Number,
    required: false,
    default: 2000,
  },
})

const tsForm = reactive({
  acknowledgementRequired: false,
  payloadType: 'CONV',
  payload: {
    evt: 'TS',
    com: '',
    ti: 0,
  },
})

const rules = reactive<FormRules>({
  'payload.car': [
    {
      required: true,
      message: '请填写载体编号',
      trigger: 'blur',
    },
  ],
  'payload.com': [
    {
      required: true,
      message: '请填写组件编号',
      trigger: 'blur',
    },
  ],
  'payload.ti': [
    {
      required: true,
      message: '请填写任务状态',
      trigger: 'blur',
    },
  ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      loading.value = true
      sendMessageByServer(tsForm)
        .then((res) => {
          ElMessage({
            showClose: true,
            message: res.data.message,
            type: 'success',
            duration: 1000,
          })
        })
        .finally(() => {
          loading.value = false
        })
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>
